package com.itheima.exception;


import com.itheima.result.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

//打上返回是一个字符串或对象不是，网址跳转，和aop异常加强注解
@RestControllerAdvice
public class MyException {
    //这里标注处理的异常类型注解
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R SqlException(SQLIntegrityConstraintViolationException e){
        String mes = e.getMessage();
        if (mes.contains("Duplicate entry")){
            return R.error(mes.split(" ")[2]+ "用户名已存在");
        }else {
            return R.error("未知错误！");
        }
    }

    @ExceptionHandler(CException.class)
    public R aa(CException e){
        return R.error(e.getMessage());
    }
}
